Clover coverage report - Enterprise Web Services - 1.0
Coverage timestamp: Mon May 30 2005 17:10:32 CEST
file stats: LOC: 507   Methods: 25
NCLOC: 332   Classes: 2
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
 
 Source file Conditionals Statements Methods TOTAL
J2eeEmitter.java 61.8% 72.8% 76% 70%
coverage coverage
 1   
 /*
 2   
  * Copyright 2001-2004 The Apache Software Foundation.
 3   
  *
 4   
  * Licensed under the Apache License, Version 2.0 (the "License");
 5   
  * you may not use this file except in compliance with the License.
 6   
  * You may obtain a copy of the License at
 7   
  *
 8   
  *      http://www.apache.org/licenses/LICENSE-2.0
 9   
  *
 10   
  * Unless required by applicable law or agreed to in writing, software
 11   
  * distributed under the License is distributed on an "AS IS" BASIS,
 12   
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13   
  * See the License for the specific language governing permissions and
 14   
  * limitations under the License.
 15   
  */
 16   
 package org.apache.geronimo.ews.jaxrpcmapping;
 17   
 
 18   
 import org.apache.axis.i18n.Messages;
 19   
 import org.apache.axis.utils.ClassUtils;
 20   
 import org.apache.axis.wsdl.gen.Generator;
 21   
 import org.apache.axis.wsdl.gen.NoopFactory;
 22   
 import org.apache.axis.wsdl.symbolTable.BindingEntry;
 23   
 import org.apache.axis.wsdl.symbolTable.CollectionElement;
 24   
 import org.apache.axis.wsdl.symbolTable.MessageEntry;
 25   
 import org.apache.axis.wsdl.symbolTable.PortTypeEntry;
 26   
 import org.apache.axis.wsdl.symbolTable.ServiceEntry;
 27   
 import org.apache.axis.wsdl.symbolTable.SymTabEntry;
 28   
 import org.apache.axis.wsdl.symbolTable.SymbolTable;
 29   
 import org.apache.axis.wsdl.symbolTable.Type;
 30   
 import org.apache.axis.wsdl.symbolTable.TypeEntry;
 31   
 import org.apache.axis.wsdl.toJava.Emitter;
 32   
 import org.apache.axis.wsdl.toJava.Namespaces;
 33   
 import org.apache.axis.wsdl.toJava.Utils;
 34   
 import org.apache.geronimo.ews.ws4j2ee.context.J2EEWebServiceContext;
 35   
 import org.apache.geronimo.ews.ws4j2ee.toWs.GenerationFault;
 36   
 
 37   
 import javax.wsdl.Binding;
 38   
 import javax.wsdl.Definition;
 39   
 import javax.xml.namespace.QName;
 40   
 import java.io.FileInputStream;
 41   
 import java.io.IOException;
 42   
 import java.io.InputStream;
 43   
 import java.util.Collection;
 44   
 import java.util.Enumeration;
 45   
 import java.util.HashMap;
 46   
 import java.util.Iterator;
 47   
 import java.util.Map;
 48   
 import java.util.Properties;
 49   
 import java.util.Vector;
 50   
 
 51   
 /**
 52   
  * @author Ias (iasandcb@tmax.co.kr)
 53   
  */
 54   
 
 55   
 public class J2eeEmitter extends Emitter {
 56   
 
 57   
     private String mappingFilePath;
 58   
     private static final int timeoutms = 45000000;
 59   
     private InputStream mappingFileInputStream;
 60   
     private QName serviceQName;
 61   
     private JaxRpcMapper jaxRpcMapper;
 62   
     private SymbolTable symbolTable;
 63   
     protected J2EEWebServiceContext wscontext;
 64   
 
 65  3
     public J2eeEmitter() {
 66  3
         J2eeGeneratorFactory factory = new J2eeGeneratorFactory(this);
 67  3
         setFactory(factory);
 68   
     } // ctor
 69   
 
 70  15
     public J2eeEmitter(J2EEWebServiceContext wscontext, J2eeGeneratorFactory factory) {
 71  15
         this.wscontext = wscontext;
 72  15
         if (factory == null)
 73  0
             factory = new J2eeGeneratorFactory(this);
 74  15
         setFactory(factory);
 75  15
         factory.setEmitter(this);
 76   
     } // ctor
 77   
 
 78  18
     public void setMappingFilePath(String mappingFilePath) {
 79  18
         this.mappingFilePath = mappingFilePath;
 80   
     }
 81   
 
 82  18
     private void loadMapping() throws GenerationFault {
 83   
         //jaxRpcMapper = new JAXBJaxRpcMapper();
 84  18
         jaxRpcMapper = new XMLBeansJaxRpcMapper();
 85  18
         if (mappingFilePath == null) {
 86  0
             jaxRpcMapper.loadMappingFromInputStream(mappingFileInputStream);
 87   
         } else {
 88  18
             jaxRpcMapper.loadMappingFromDir(mappingFilePath);
 89   
         }
 90  18
         setNowrap(!jaxRpcMapper.hasWrappedElement());
 91  18
         int length = jaxRpcMapper.getPackageMappingCount();
 92  18
         Map namespaceMap = getNamespaceMap();
 93  18
         for (int i = 0; i < length; i++) {
 94  18
             namespaceMap.put(jaxRpcMapper.getPackageMappingURI(i), jaxRpcMapper.getPackageMappingClassName(i));
 95   
         }
 96   
     }
 97   
 
 98   
     /**
 99   
      * Emit appropriate Java files for a WSDL at a given URL.
 100   
      * This method will time out after the number of milliseconds specified
 101   
      * by our timeoutms member.
 102   
      */
 103  4
     public void run(String wsdlURL) throws Exception {
 104  4
         setup();
 105  4
         runTemp(wsdlURL);
 106   
     } // run
 107   
 
 108  14
     public void runServerSide(String wsdlURL) throws Exception {
 109  14
         setup();
 110  14
         if (getFactory() == null) {
 111  0
             setFactory(new NoopFactory());
 112   
         }
 113  14
         symbolTable = new SymbolTable(getFactory().getBaseTypeMapping(),
 114   
                 imports,
 115   
                 verbose,
 116   
                 nowrap);
 117  14
         symbolTable.setQuiet(quiet);
 118  14
         symbolTable.setWrapArrays(wrapArrays);
 119  14
         symbolTable.populate(wsdlURL, username, password);
 120  14
         generate(symbolTable);
 121   
     }
 122   
 
 123  146
     public SymbolTable getSymbolTable() {
 124  146
         return symbolTable;
 125   
     }
 126   
 
 127  18
     private void setup() throws GenerationFault {
 128  18
         try {
 129  18
             if (baseTypeMapping == null) {
 130  18
                 setTypeMappingVersion(typeMappingVersion);
 131   
             }
 132  18
             getFactory().setBaseTypeMapping(baseTypeMapping);
 133  18
             namespaces = new Namespaces(getOutputDir());
 134  18
             if (getPackageName() != null) {
 135  0
                 namespaces.setDefaultPackage(getPackageName());
 136   
             } else {
 137   
                 // First, read the namespace mapping file - configurable, by default
 138   
                 // NStoPkg.properties - if it exists, and load the namespaceMap HashMap
 139   
                 // with its data.
 140  18
                 getNStoPkgFromPropsFile(namespaces);
 141  18
                 loadMapping();
 142  18
                 if (getNamespaceMap() != null) {
 143  18
                     namespaces.putAll(getNamespaceMap());
 144   
                 }
 145   
             }
 146   
         } catch (IOException e) {
 147  0
             e.printStackTrace();
 148  0
             throw GenerationFault.createGenerationFault(e);
 149   
         }
 150   
     } // setup
 151   
 
 152   
     /**
 153   
      * Tries to load the namespace-to-package mapping file.
 154   
      * <ol>
 155   
      * <li>if a file name is explicitly set using <code>setNStoPkg()</code>, tries
 156   
      * to load the mapping from this file. If this fails, the built-in default
 157   
      * mapping is used.
 158   
      * <li>if no file name is set, tries to load the file <code>DEFAULT_NSTOPKG_FILE</code>
 159   
      * as a java resource. If this fails, the built-in dfault mapping is used.
 160   
      * </ol>
 161   
      *
 162   
      * @param namespaces a hashmap which is filled with the namespace-to-package mapping
 163   
      *                   in this method
 164   
      * @see #setNStoPkg(String)
 165   
      * @see #DEFAULT_NSTOPKG_FILE
 166   
      * @see org.apache.axis.utils.ClassUtils#getResourceAsStream(java.lang.Class,String)
 167   
      */
 168  18
     private void getNStoPkgFromPropsFile(HashMap namespaces) throws IOException {
 169  18
         Properties mappings = new Properties();
 170  18
         if (NStoPkgFilename != null) {
 171  0
             try {
 172  0
                 mappings.load(new FileInputStream(NStoPkgFilename));
 173  0
                 if (verbose) {
 174  0
                     System.out.println(Messages.getMessage("nsToPkgFileLoaded00", NStoPkgFilename));
 175   
                 }
 176   
             } catch (Throwable t) {
 177   
                 // loading the custom mapping file failed. We do not try
 178   
                 // to load the mapping from a default mapping file.
 179  0
                 throw new IOException(Messages.getMessage("nsToPkgFileNotFound00", NStoPkgFilename));
 180   
             }
 181   
         } else {
 182  18
             try {
 183  18
                 mappings.load(new FileInputStream(DEFAULT_NSTOPKG_FILE));
 184  0
                 if (verbose) {
 185  0
                     System.out.println(Messages.getMessage("nsToPkgFileLoaded00", DEFAULT_NSTOPKG_FILE));
 186   
                 }
 187   
             } catch (Throwable t) {
 188  18
                 try {
 189  18
                     mappings.load(ClassUtils.getResourceAsStream(Emitter.class, DEFAULT_NSTOPKG_FILE));
 190  0
                     if (verbose) {
 191  0
                         System.out.println(Messages.getMessage("nsToPkgDefaultFileLoaded00", DEFAULT_NSTOPKG_FILE));
 192   
                     }
 193   
                 } catch (Throwable t1) {
 194   
                     // loading the default mapping file failed.
 195   
                     // The built-in default mapping is used
 196   
                     // No message is given, since this is generally what happens
 197   
                 }
 198   
             }
 199   
         }
 200  18
         Enumeration keys = mappings.propertyNames();
 201  18
         while (keys.hasMoreElements()) {
 202  0
             String key = (String) keys.nextElement();
 203  0
             namespaces.put(key, mappings.getProperty(key));
 204   
         }
 205   
     } // getNStoPkgFromPropsFile
 206   
 
 207   
     /**
 208   
      * Convert the specified QName into a full Java Name.
 209   
      */
 210  320
     public String getJavaName(QName qName) {
 211   
         // If this is one of our special 'collection' qnames.
 212   
 
 213   
         // get the element type and append []
 214   
 
 215  320
         if (qName.getLocalPart().indexOf("[") > 0) {
 216  0
             String localPart = qName.getLocalPart().substring(0, qName.getLocalPart().indexOf("["));
 217  0
             QName eQName = new QName(qName.getNamespaceURI(), localPart);
 218  0
             return getJavaName(eQName) + "[]";
 219   
         }
 220   
 
 221   
 
 222   
 
 223   
         // Handle the special "java" namespace for types
 224   
 
 225  320
         if (qName.getNamespaceURI().equalsIgnoreCase("java")) {
 226  0
             return qName.getLocalPart();
 227   
         }
 228   
 
 229   
 
 230   
 
 231   
         // The QName may represent a base java name, so check this first
 232   
 
 233  320
         String fullJavaName = getFactory().getBaseTypeMapping().getBaseName(qName);
 234  320
         if (fullJavaName != null)
 235  117
             return fullJavaName;
 236   
 
 237   
 
 238   
 
 239   
         // Use the namespace uri to get the appropriate package
 240   
 
 241  203
         String pkg = getPackage(qName.getNamespaceURI());
 242  203
         if (pkg != null) {
 243  203
             fullJavaName = pkg + "." + Utils.xmlNameToJavaClass(qName.getLocalPart());
 244   
         } else {
 245  0
             fullJavaName = Utils.xmlNameToJavaClass(qName.getLocalPart());
 246   
         }
 247  203
         return fullJavaName;
 248   
     } // getJavaName
 249   
 
 250   
     /**
 251   
      * Parse a WSDL at a given URL.
 252   
      * This method will time out after the number of milliseconds specified
 253   
      * by our timeoutms member.
 254   
      */
 255  4
     public void runTemp(String wsdlURI) throws Exception {
 256  4
         if (getFactory() == null) {
 257  0
             setFactory(new NoopFactory());
 258   
         }
 259  4
         symbolTable = new SymbolTable(getFactory().getBaseTypeMapping(),
 260   
                 imports,
 261   
                 verbose,
 262   
                 nowrap);
 263  4
         symbolTable.setQuiet(quiet);
 264  4
         symbolTable.setWrapArrays(wrapArrays);
 265   
 // We run the actual Emitter in a thread that we can kill
 266  4
         WSDLRunnable runnable = new WSDLRunnable(symbolTable, wsdlURI);
 267  4
         Thread wsdlThread = new Thread(runnable);
 268  4
         wsdlThread.start();
 269  4
         try {
 270  4
             if (timeoutms > 0)
 271  4
                 wsdlThread.join(timeoutms);
 272   
             else
 273  0
                 wsdlThread.join();
 274   
         } catch (InterruptedException e) {
 275   
         }
 276  4
         if (wsdlThread.isAlive()) {
 277  0
             wsdlThread.interrupt();
 278  0
             throw new IOException(Messages.getMessage("timedOut"));
 279   
         }
 280  4
         if (runnable.getFailure() != null) {
 281  0
             throw runnable.getFailure();
 282   
         }
 283   
     } // run
 284   
 
 285   
     private class WSDLRunnable implements Runnable {
 286   
         private SymbolTable symbolTable;
 287   
         private String wsdlURI;
 288   
         private Exception failure = null;
 289   
 
 290  4
         public WSDLRunnable(SymbolTable symbolTable, String wsdlURI) {
 291  4
             this.symbolTable = symbolTable;
 292  4
             this.wsdlURI = wsdlURI;
 293   
         } // ctor
 294   
 
 295  4
         public void run() {
 296  4
             try {
 297  4
                 symbolTable.populate(wsdlURI, username, password);
 298  4
                 generate(symbolTable);
 299   
             } catch (Exception e) {
 300  0
                 failure = e;
 301   
             }
 302   
         } // run
 303   
 
 304  4
         public Exception getFailure() {
 305  4
             return failure;
 306   
         } // getFailure
 307   
     } // WSDLRunnable
 308   
 
 309  18
     protected void sanityCheck(SymbolTable symbolTable) {
 310  18
         Iterator it = symbolTable.getHashMap().values().iterator();
 311  18
         while (it.hasNext()) {
 312  299
             Vector v = (Vector) it.next();
 313  299
             for (int i = 0; i < v.size(); ++i) {
 314  299
                 SymTabEntry entry = (SymTabEntry) v.elementAt(i);
 315  299
                 String namespace = entry.getQName().getNamespaceURI();
 316  299
                 String packageName =
 317   
                         org.apache.axis.wsdl.toJava.Utils.makePackageName(namespace);
 318  299
                 String localName = entry.getQName().getLocalPart();
 319  299
                 if (localName.equals(packageName) &&
 320   
                         packageName.equals(namespaces.getCreate(namespace))) {
 321  0
                     packageName += "_pkg";
 322  0
                     namespaces.put(namespace, packageName);
 323   
                 }
 324   
             }
 325   
         }
 326   
     }
 327   
 
 328  18
     private void generate(SymbolTable symbolTable) throws IOException {
 329  18
         sanityCheck(symbolTable);
 330  18
         Definition def = symbolTable.getDefinition();
 331  18
         getFactory().generatorPass(def, symbolTable);
 332  18
         if (isDebug()) {
 333  0
             symbolTable.dump(System.out);
 334   
         }
 335  18
         if (getOutputDir() == null) {
 336  0
             return;
 337   
         }
 338   
 
 339   
         // Generate bindings for types
 340  18
         generateTypes(symbolTable);
 341  18
         Iterator it = symbolTable.getHashMap().values().iterator();
 342  18
         while (it.hasNext()) {
 343  299
             Vector v = (Vector) it.next();
 344  299
             for (int i = 0; i < v.size(); ++i) {
 345  299
                 SymTabEntry entry = (SymTabEntry) v.elementAt(i);
 346  299
                 Generator gen = null;
 347  299
                 if (entry instanceof MessageEntry) {
 348  122
                     gen = getFactory().getGenerator(((MessageEntry) entry).getMessage(), symbolTable);
 349  177
                 } else if (entry instanceof PortTypeEntry) {
 350  18
                     PortTypeEntry pEntry = (PortTypeEntry) entry;
 351   
                     // If the portType is undefined, then we're parsing a Definition
 352   
                     // that didn't contain a portType, merely a binding that referred
 353   
                     // to a non-existent port type.  Don't bother writing it.
 354  18
                     if (pEntry.getPortType().isUndefined()) {
 355  0
                         continue;
 356   
                     }
 357  18
                     gen = getFactory().getGenerator(pEntry.getPortType(), symbolTable);
 358  159
                 } else if (entry instanceof BindingEntry) {
 359  18
                     BindingEntry bEntry = (BindingEntry) entry;
 360  18
                     Binding binding = bEntry.getBinding();
 361   
 
 362   
                     // If the binding is undefined, then we're parsing a Definition
 363   
                     // that didn't contain a binding, merely a service that referred
 364   
                     // to a non-existent binding.  Don't bother writing it.
 365  18
                     if (binding.isUndefined() || !bEntry.isReferenced()) {
 366  0
                         continue;
 367   
                     }
 368  18
                     gen = getFactory().getGenerator(binding, symbolTable);
 369  141
                 } else if (entry instanceof ServiceEntry) {
 370  18
                     ServiceEntry sEntry = (ServiceEntry) entry;
 371  18
                     serviceQName = sEntry.getService().getQName();
 372  18
                     gen = getFactory().getGenerator(sEntry.getService(), symbolTable);
 373   
                 }
 374  299
                 if (gen != null) {
 375  176
                     gen.generate();
 376   
                 }
 377   
             }
 378   
         }
 379   
 
 380   
         // Output extra stuff (deployment files and faults)
 381   
         // outside of the recursive emit method.
 382  18
         Generator gen = getFactory().getGenerator(def, symbolTable);
 383  18
         gen.generate();
 384   
     } // generate
 385   
 
 386   
     /**
 387   
      * Generate bindings (classes and class holders) for the complex types.
 388   
      * If generating serverside (skeleton) spit out beanmappings
 389   
      */
 390  18
     private void generateTypes(SymbolTable symbolTable) throws IOException {
 391  18
         Map elements = symbolTable.getElementIndex();
 392  18
         Collection elementCollection = elements.values();
 393  18
         for (Iterator i = elementCollection.iterator(); i.hasNext();) {
 394  0
             TypeEntry type = (TypeEntry) i.next();
 395  0
             type.setOnlyLiteralReference(false);
 396   
 
 397   
             // Write out the type if and only if:
 398   
             //  - we found its definition (getNode())
 399   
             //  - it is referenced
 400   
             //  - it is not a base type
 401   
             //  - it is a Type (not an Element) or a CollectionElement
 402   
             // (Note that types that are arrays are passed to getGenerator
 403   
             //  because they may require a Holder)
 404   
 
 405   
             // A CollectionElement is an array that might need a holder
 406   
 
 407  0
             boolean isType = ((type instanceof Type)
 408   
                              || (type instanceof CollectionElement));
 409   
 
 410  0
                      if ((type.getNode() != null)
 411   
                     && !type.getNode().getLocalName().equals("attributeGroup")
 412   
                     && !type.getNode().getLocalName().equals("group")
 413   
                              && type.isReferenced() && isType
 414   
                              && (type.getBaseType() == null)) {
 415  0
                          Generator gen = getFactory().getGenerator(type, symbolTable);
 416   
 
 417  0
                          gen.generate();
 418   
                      }
 419   
         }
 420  18
         Map types = symbolTable.getTypeIndex();
 421  18
         Collection typeCollection = types.values();
 422  18
         for (Iterator i = typeCollection.iterator(); i.hasNext();) {
 423  105
             TypeEntry type = (TypeEntry) i.next();
 424   
 
 425   
             // Write out the type if and only if:
 426   
             //  - we found its definition (getNode())
 427   
             //  - it is referenced
 428   
             //  - it is not a base type
 429   
             //  - it is a Type (not an Element) or a CollectionElement
 430   
             // (Note that types that are arrays are passed to getGenerator
 431   
             //  because they may require a Holder)
 432   
 
 433   
             // A CollectionElement is an array that might need a holder
 434   
 
 435  105
             boolean isType = (type instanceof Type || type instanceof CollectionElement);
 436  105
             if (type.getNode() != null
 437   
                     && !type.getNode().getLocalName().equals("attributeGroup")
 438   
                     && !type.getNode().getLocalName().equals("group") &&
 439   
                     type.isReferenced() &&
 440   
                     isType &&
 441   
                     type.getBaseType() == null) {
 442  40
                 Generator gen = getFactory().getGenerator(type, symbolTable);
 443  40
                 gen.generate();
 444   
             }
 445   
         }
 446   
     } // generateTypes
 447   
 
 448   
     /**
 449   
      * @param mappingFileInputStream The mappingFileInputStream to set.
 450   
      */
 451  0
     public void setMappingFileInputStream(InputStream mappingFileInputStream) {
 452  0
         this.mappingFileInputStream = mappingFileInputStream;
 453   
     }
 454   
 
 455   
     /**
 456   
      * @return Returns the serviceQName.
 457   
      */
 458  0
     public QName getServiceQName() {
 459  0
         return serviceQName;
 460   
     }
 461   
 
 462   
     /**
 463   
      * @return
 464   
      */
 465  0
     public boolean isGeneratingInterface() {
 466   
         // TODO Auto-generated method stub
 467  0
         return false;
 468   
     }
 469   
 
 470   
     /**
 471   
      * @return
 472   
      */
 473  0
     public boolean isGeneratingTypes() {
 474   
         // TODO Auto-generated method stub
 475  0
         return false;
 476   
     }
 477   
 
 478   
     /**
 479   
      * @return Returns the jaxRpcMapper.
 480   
      */
 481  38
     public JaxRpcMapper getJaxRpcMapper() {
 482  38
         return jaxRpcMapper;
 483   
     }
 484   
 
 485   
     /**
 486   
      * @param jaxRpcMapper The jaxRpcMapper to set.
 487   
      */
 488  0
     public void setJaxRpcMapper(JaxRpcMapper jaxRpcMapper) {
 489  0
         this.jaxRpcMapper = jaxRpcMapper;
 490   
     }
 491   
 
 492   
     /**
 493   
      * @return
 494   
      */
 495  15
     public J2EEWebServiceContext getWscontext() {
 496  15
         return wscontext;
 497   
     }
 498   
 
 499   
     /**
 500   
      * @param context
 501   
      */
 502  0
     public void setWscontext(J2EEWebServiceContext context) {
 503  0
         wscontext = context;
 504   
     }
 505   
 
 506   
 } // class MyEmitter
 507